package com.dhy.controller;

import com.dhy.pojo.Department;
import com.dhy.query.DepartmentQuery;
import com.dhy.service.IDepartmentService;
import com.dhy.utils.AjaxResult;
import com.dhy.utils.annotation.MyPermissionAnnotation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/departments")
@CrossOrigin
@Api(value = "部门的API,部门相关的功能")
@MyPermissionAnnotation(name = "部门管理")
public class DepartmentController {

    @Autowired
    IDepartmentService iDepartmentService;

    //添加和修改共用一个接口
    @PostMapping
    @MyPermissionAnnotation(name = "部门新增和修改")
    public AjaxResult insertOrUpdate(@RequestBody Department department) {
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        try {
            if (department.getId() == null) {
                //执行修改方法
                iDepartmentService.insert(department);
                ajaxResult.setMessage("操作成功").setSuccess(true);
            } else {
                //执行增加方法
                iDepartmentService.update(department);
                ajaxResult.setMessage("操作成功").setSuccess(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setMessage("操作失败").setSuccess(false);
        }
        return ajaxResult;
    }

    //删除接口
    @RequestMapping(path = "/{id}", method = RequestMethod.DELETE)
    @MyPermissionAnnotation(name = "部门删除")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        try {
            iDepartmentService.deleteById(id);
            ajaxResult.setMessage("操作成功").setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setMessage("操作失败").setSuccess(false);
        }
        return ajaxResult;
    }

    //id查询接口
    /*
     * 传递单个参数的方法
     * */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    public AjaxResult selectById(@ApiParam(name = "编号", required = true) @PathVariable("id") Long id) {
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        try {
            Department department = iDepartmentService.selectById(id);
            if (department == null) {
                ajaxResult.setMessage("操作失败").setSuccess(false);
            } else {
                ajaxResult.setMessage("操作成功").setSuccess(true).setData(department);
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setMessage("操作失败").setSuccess(false);
        }
        return ajaxResult;
    }

    //name查询接口
    /*
     * 区分同种请求方法：增加小路径
     * */
    @GetMapping("/name/{name}")
    public AjaxResult selectByName(@PathVariable("name") String name) {
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();

        return ajaxResult;
    }

    //id，name共同查询接口
    /*
     * 传递多个参数的方法
     * */
    @GetMapping("/id/{id}/name/{name}")
    public AjaxResult selectByNameAndId(@PathVariable("id") Long id, @PathVariable("name") String name) {
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();

        return ajaxResult;
    }

    @GetMapping
    public AjaxResult selectAll() {
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        try {
            List<Department> departments = iDepartmentService.selectAll();
            return ajaxResult.setMessage("操作成功").setSuccess(true).setData(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return ajaxResult.setSuccess(false).setMessage("操作失败");
        }
    }

    @GetMapping("/selectPageSize")
    public AjaxResult selectPageSize() {
        System.out.println("开始方法");
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        try {
            Long pageSize = iDepartmentService.selectPageSize();
            return ajaxResult.setMessage("操作成功").setSuccess(true).setData(pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return ajaxResult.setSuccess(false).setMessage("操作失败");
        }
    }

    @MyPermissionAnnotation(name = "部门分页查询")
    @PostMapping("/limitSelectAll")
    public AjaxResult selectLimit(@RequestBody DepartmentQuery departmentQuery) {
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        try {
            List<Department> departments = iDepartmentService.selectLimit(departmentQuery);
            return ajaxResult.setMessage("操作成功").setSuccess(true).setData(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return ajaxResult.setSuccess(false).setMessage("操作失败");
        }
    }

    @PostMapping("/keyWord")
    public AjaxResult selectByKey(@RequestBody DepartmentQuery departmentQuery) {
        System.out.println("1111111111111111");
        AjaxResult ajaxResult = AjaxResult.ajaxResultBuild();
        try {
            List<Department> departments = iDepartmentService.selectByKey(departmentQuery);
            return ajaxResult.setMessage("操作成功").setSuccess(true).setData(departments);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setMessage("操作失败").setSuccess(false);
        }
        return ajaxResult;
    }

}
